# Send message to log
log_code()


# ches 2019
df_id <- read.csv("data/CHES2019V3.csv") %>% 
  dplyr::select(country, eastwest, party_id) %>% 
  distinct()
df_pid <- read.csv("data/1999-2019_CHES_dataset_means(v1).csv")[,1:14] %>% 
  filter(year == 2019)

df <- read.csv("data/CHES2019_experts.csv") %>% 
  merge(df_id, by = "party_id") %>% 
  filter({eastwest == 1 & !(country %in% c("cyp", "lux"))} | {is.na(eastwest) & !(country %in% c("tur", "swi", "nor", "ice"))}) %>% 
  dplyr::select(country:eastwest, 
                party_id:party_name,
                immigrate_policy, multiculturalism, redistribution, environment, spendvtax, deregulation, econ_interven, civlib_laworder, sociallifestyle, religious_principles,
                ethnic_minorities, nationalism, urban_rural, protectionism, regions, # people_vs_elite, 
                eu_position = position, eu_cohesion, eu_foreign, eu_intmark, eu_budgets, eu_asylum, lrgen, lrecon, galtan) %>% 
  mutate(expert = paste0(country, ".", id))

df$lrgen[df$lrgen == -1] <- NA
df$lrecon[df$lrecon == -1] <- NA
df$galtan[df$galtan == -1] <- NA
df$lrecon[df$lrecon %in% c("", ".d")] <- NA
df$galtan[df$galtan %in% c("", ".d")] <- NA
df$lrecon <- as.numeric(df$lrecon)
df$galtan <- as.numeric(df$galtan)

df[,7:21] <- df[,7:21] + 1

p_id <- 1:length(unique(df$party_id))
c_id <- 1:length(unique(df$country))
e_id <- 1:length(unique(df$expert))
i_id <- 1:21

names(p_id) <- as.character(unique(df$party_id))
names(c_id) <- unique(df$country)
names(e_id) <- unique(df$expert)
names(i_id) <- colnames(df[,7:27])

df$p_id <- p_id[as.character(df$party_id)]
df$c_id <- c_id[df$country]
df$e_id <- e_id[df$expert]

p_start <- df %>% 
  select(p_id, lrgen, lrecon, galtan) %>% 
  group_by(p_id) %>% 
  summarize(lr = median(lrgen, na.rm = T),
            lrecon = median(lrecon, na.rm = T),
            galtan = median(galtan, na.rm = T)) %>% 
  ungroup()
p_start[,2:4] <- apply(p_start[,2:4], 2, function(x) (x - mean(x)) / sd(x))
p_start <- p_start %>% 
  arrange(p_id)

make_parameter <- function(df){
  df <- df %>% 
    mutate(parameter = as.factor(gsub("\\[.*]", "", variable)))
  df
}

# main scenario ####
load("out/full1.Rda")
full_df <- make_parameter(df_full) %>% 
  filter(parameter %in% c("theta", "beta_1", "beta_2", "idio", "exp_scale", "scale_beta", "shift_alpha", "mu_alpha", "res_alpha"))

full_df <- bind_rows(
  full_df %>% 
    filter(parameter %in% c("exp_scale")) %>% 
    mutate(info = names(e_id)),
  full_df %>% 
    filter(parameter %in% c("scale_beta")) %>% 
    mutate(info = names(c_id)),
  full_df %>% 
    filter(parameter %in% c("shift_alpha")) %>% 
    mutate(info = names(c_id)),
  full_df %>% 
    filter(parameter %in% c("res_alpha")) %>% 
    mutate(item = rep(names(i_id), length.out = 315),
           info = rep(names(c_id), each = 21, length.out = 315)),
  full_df %>% 
    filter(parameter %in% c("beta_1", "beta_2")) %>% 
    mutate(item = rep(names(i_id), length.out = 630),
           info = rep(names(c_id), each = 21, length.out = 630)),
  full_df %>% 
    filter(parameter %in% c("theta")) %>% 
    mutate(party = rep(names(p_id), length.out = 256),
           p_id = rep((p_id), length.out = 256)) %>% 
    merge(df %>% select(p_id, country) %>% distinct(), sort = FALSE), # replace?!
  full_df %>% 
    filter(parameter %in% c("idio")) %>% 
    mutate(item = rep(names(i_id), length.out = 2688),
           party = rep(names(p_id), each = length(i_id), length.out = 2688),
           p_id = rep((p_id), each = length(i_id), length.out = 2688))
)

save(p_start, full_df, df_pid, file = "out/full2.Rda")

# alternative scenarios ####
load("out/m0.Rda")
load("out/m1.Rda")
load("out/m2.Rda")
load("out/m3.Rda")
load("out/m4.Rda")

p_acc <- bind_rows(df_m0[df_m0$variable=="p_share",],
                   df_m1[df_m1$variable=="p_share",],
                   df_m2[df_m2$variable=="p_share",],
                   df_m3[df_m3$variable=="p_share",],
                   df_m4[df_m4$variable=="p_share",],
                   df_full[df_full$variable=="p_share",]) %>% 
  mutate(model = c("m0", "m1", "m2", "m3", "m4", "m5"))

loo <- bind_rows(loo_m0$estimates[1,],
                   loo_m1$estimates[1,],
                   loo_m2$estimates[1,],
                   loo_m3$estimates[1,],
                   loo_m4$estimates[1,],
                   loo_full$estimates[1,]) %>% 
  mutate(model = c("m0", "m1", "m2", "m3", "m4", "m5"))

save(p_acc, loo, file = "out/comp.Rda")

rm(list = setdiff(ls(), "lf"))
